package com.sx.controller;

import com.aliyuncs.utils.StringUtils;
import com.sx.basic.pageable.PageRequest;
import com.sx.basic.ResponseEntity;
import com.sx.basic.pageable.Condition;
import com.sx.design.IDesignModuleService;
import com.sx.design.IDesignParamsService;
import com.sx.design.IPageService;
import com.sx.entity.design.DesignModuleEntity;
import com.sx.entity.design.PageEntity;
import com.sx.mapping.DesignMapping;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author AdinZ
 * @date 2020/11/12 17:48
 * 页面设计
 */
@RestController
public class DesignController {
    @Resource private IPageService pageService;
    @Resource private IDesignModuleService designModuleService;
    @Resource private IDesignParamsService designParamsService;

    @GetMapping(DesignMapping.DESIGN_PAGE) @ApiOperation("页面数据列表")
    public ResponseEntity<List<PageEntity>> pagePage(@RequestParam("page")int page,
                                                     @RequestParam("size")int size){
        return this.pageService.findPage(PageRequest.of(page, size, Sort.Direction.DESC,"id"));
    }

    @PutMapping(DesignMapping.DESIGN_PAGE) @ApiOperation("添加修改页面数据")
    public ResponseEntity putPage(@RequestBody PageEntity entity,
                                  @RequestParam(value = "action",required = false)String action){
        if(entity==null)return new ResponseEntity().toError();

        return this.pageService.putDetail(action,entity);
    }

    @DeleteMapping(DesignMapping.DESIGN_PAGE)@ApiOperation("删除页面")
    public ResponseEntity deletePage(@RequestParam("id")Long[] ids){
        if(ids==null)return new ResponseEntity().toError();
        this.pageService.delete(ids);
        for(long id:ids) {
            this.designParamsService.deleteByModuleId(id);
        }
        return new ResponseEntity().toSuccess();
    }

    @GetMapping(DesignMapping.DESIGN_PAGE_GET)
    public ResponseEntity pageDetail(@PathVariable("id")Long id){
        if(id==null)return new ResponseEntity().toError();
        return this.pageService.pageDetail(id);
    }


    @GetMapping(DesignMapping.DESIGN_MODULE) @ApiOperation("组件模板列表")
    public ResponseEntity<List<DesignModuleEntity>> pageDesignModule(@RequestParam("page")int page,
                                                                     @RequestParam("size")int size){
        return this.designModuleService.findPage(PageRequest.of(page, size));
    }

    @PutMapping(DesignMapping.DESIGN_MODULE) @ApiOperation("添加修改组件模板")
    public ResponseEntity putDesignModule(@RequestBody DesignModuleEntity entity){
        if(entity==null)return new ResponseEntity().toError();
        return this.designModuleService.putDesignModule(entity);
    }

    @GetMapping(DesignMapping.DESIGN_MODULE_GET)@ApiOperation("获取组件模板数据")
    public ResponseEntity<DesignModuleEntity> getDesignModule(@PathVariable("id")Long id){
        DesignModuleEntity entity=  this.designModuleService.getDetail(id);
        return new ResponseEntity<>().toSuccess(entity);
    }

    @DeleteMapping(DesignMapping.DESIGN_MODULE) @ApiOperation("删除组件模板")
    public ResponseEntity deleteDesignModule(@RequestParam("id")Long[] ids){
        if(ids==null)return new ResponseEntity().toError();
        this.designModuleService.delete(ids);
        return new ResponseEntity().toSuccess();
    }



}
